package com.shyroke.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.shyroke.entity.FansKey;
import com.shyroke.entity.ShowUser;
import com.shyroke.entity.User;
import com.shyroke.entity.UserInfo;
import com.shyroke.service.FansServiceI;
import com.shyroke.service.UserInfoServiceI;
import com.shyroke.service.UserServiceI;

@Controller
@RequestMapping(value="/tongxunAction")
public class TongXunAction {
	@Autowired
	private FansServiceI fansService;
	
	@Autowired
	private UserServiceI userService;
	
	@Autowired
	private UserInfoServiceI userInfoService;
	
	/**
	 * 跳转到通讯录页面
	 * @return
	 */
	@RequestMapping(value="/tongxunView")
	public String tongxunView() {
		return "tongxunlu/main";
	}
	
	
	/**
	 * 跳转到查看关注好友的页面
	 * @param model
	 * @param session
	 * @param type 如果不为空则表示查看所有粉丝
	 * @return
	 */
	@RequestMapping(value="/guanzhuView")
	public ModelAndView guanzhuView(ModelAndView model,HttpSession session,
			@RequestParam(value="type",required=false)String type) {
		User user=(User)session.getAttribute("user");
		//获取关注的用户的id集合
		List<FansKey> guanzhuList=null;
		if(type!=null) {
			guanzhuList=fansService.selectByUserId(user.getId());
		}else {
			guanzhuList=fansService.selectByFansId(user.getId());
		}
		
		User guanzhuUser=null;
		UserInfo userInfo=null;
		ShowUser showUser=null;
		List<ShowUser> showUserList=new ArrayList<>();
		for(FansKey guanzhu:guanzhuList) {
			showUser=new ShowUser();
			
			if(type!=null) {
				//查看粉丝
				guanzhuUser=userService.selectByUserId(guanzhu.getFansid());
				showUser.setId(guanzhu.getFansid());
			}else {
				//查看我关注的馆友
				guanzhuUser=userService.selectByUserId(guanzhu.getUserid());
				showUser.setId(guanzhu.getUserid());
			}
			
			
			showUser.setUsername(guanzhuUser.getUsername());
			userInfo=userInfoService.selectByUserId(guanzhu.getUserid());
			showUser.setJianjie(userInfo.getJianjie());
			showUser.setTouxiangPath(userInfo.getTouxiangPath());
			showUserList.add(showUser);
		}
		
		model.addObject("showUserList", showUserList);
		
		model.setViewName("tongxunlu/guanzhu");
		
		return model;
	}
	
	/**
	 * 跳转到查看所有粉丝页面
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/fansView")
	public ModelAndView fansView(ModelAndView model,HttpSession session) {
		
		User user=(User)session.getAttribute("user");
		List<ShowUser> showUserList=new ArrayList<>();
		
		model.addObject("showUserList", showUserList);
		model.setViewName("tongxunlu/guanzhu");
		return model;
	}
	
}
